package figure8.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;

import figure8.Controller;

/**
 * This class is a menu bar and sets up all the buttons for the menu bar as well
 * as making listeners for all the buttons that forward commands to the
 * controller.
 */
public class MenuBar extends JMenuBar {
    private Controller controller;
    
	// This is a bit of a hack to make storing all the different buttons nicer,
    // this way there isn't a member variable for every single button. Might be
    // worth changing this near the end when the GUI is finalised and we can see
    // if it's necessary.
    private Map<MenuItems, JMenuItem> menuItems;

    public MenuBar(final Controller controller) {
        super();
        
        this.controller = controller;
        this.menuItems = new HashMap<MenuItems, JMenuItem>();
        
        // Create the project menu
        JMenu project = new JMenu("Project");
        project.setMnemonic(KeyEvent.VK_P);
        
        // Create all the project menu items and add listeners to them which
        // forward the commands to the controller.
        this.menuItems.put(MenuItems.NEW, new JMenuItem("New"));
        this.menuItems.get(MenuItems.NEW).setMnemonic(KeyEvent.VK_N);
        this.menuItems.get(MenuItems.NEW).setAccelerator(KeyStroke.getKeyStroke(
        		KeyEvent.VK_N, ActionEvent.ALT_MASK));
        this.menuItems.get(MenuItems.NEW).addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        controller.new_();
                    }
                });
        
        this.menuItems.put(MenuItems.OPEN, new JMenuItem("Open"));
        this.menuItems.get(MenuItems.OPEN).setMnemonic(KeyEvent.VK_O);
        this.menuItems.get(MenuItems.OPEN).setAccelerator(KeyStroke.getKeyStroke(
        		KeyEvent.VK_O, ActionEvent.ALT_MASK));
        this.menuItems.get(MenuItems.OPEN).addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        controller.open();
                    }
                });
        
        this.menuItems.put(MenuItems.SAVE, new JMenuItem("Save"));
        this.menuItems.get(MenuItems.SAVE).setMnemonic(KeyEvent.VK_S);
        this.menuItems.get(MenuItems.SAVE).setAccelerator(KeyStroke.getKeyStroke(
        		KeyEvent.VK_S, ActionEvent.ALT_MASK));
        this.menuItems.get(MenuItems.SAVE).addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        controller.save();
                    }
                });
        
        this.menuItems.put(MenuItems.SEQUENCER, new JMenuItem("Sequencer"));
        this.menuItems.get(MenuItems.SEQUENCER).addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                    	controller.openSequencer();
                    }
                });
        
        this.menuItems.put(MenuItems.CONNECT, new JMenuItem("Connect"));
        this.menuItems.get(MenuItems.CONNECT).setMnemonic(KeyEvent.VK_C);
        this.menuItems.get(MenuItems.CONNECT).addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        new NetworkConnectWindow(controller);
                    }
                });
        
        this.menuItems.put(MenuItems.HOST, new JMenuItem("Host"));
        this.menuItems.get(MenuItems.HOST).setMnemonic(KeyEvent.VK_H);
        this.menuItems.get(MenuItems.HOST).addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                    	new NetworkHostWindow(controller);
                    }
                });
        
        this.menuItems.put(MenuItems.DISCONNECT, new JMenuItem("Disconnect"));
        this.menuItems.get(MenuItems.DISCONNECT).setMnemonic(KeyEvent.VK_D);
        this.menuItems.get(MenuItems.DISCONNECT).addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                    	controller.disconnect();
                    }
                });
        
        this.menuItems.get(MenuItems.DISCONNECT).setEnabled(false);
        
    /*    this.menuItems.put(MenuItems.EXPORT, new JMenuItem("Export"));
        this.menuItems.get(MenuItems.EXPORT).setMnemonic(KeyEvent.VK_E);
        this.menuItems.get(MenuItems.EXPORT).addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        controller.export();
                    }
                });*/
        
        // Disable save and export by default, until a project is loaded
        this.menuItems.get(MenuItems.SAVE).setEnabled(false);
        this.menuItems.get(MenuItems.HOST).setEnabled(false);
     //   this.menuItems.get(MenuItems.EXPORT).setEnabled(false);
        
        this.menuItems.put(MenuItems.EXIT, new JMenuItem("Exit"));
        this.menuItems.get(MenuItems.EXIT).setMnemonic(KeyEvent.VK_X);
        this.menuItems.get(MenuItems.EXIT).setAccelerator(KeyStroke.getKeyStroke(
        		KeyEvent.VK_X, ActionEvent.ALT_MASK));
        this.menuItems.get(MenuItems.EXIT).addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        controller.exit();
                    }
                });
        
        // Add the items and category separators to the project menu.
        project.add(this.menuItems.get(MenuItems.NEW));
        project.add(this.menuItems.get(MenuItems.OPEN));
        project.add(this.menuItems.get(MenuItems.SEQUENCER));
        project.add(this.menuItems.get(MenuItems.CONNECT));
        project.add(new JSeparator());
        project.add(this.menuItems.get(MenuItems.SAVE));
        project.add(this.menuItems.get(MenuItems.HOST));
      //  project.add(this.menuItems.get(MenuItems.EXPORT));
        project.add(new JSeparator());
        project.add(this.menuItems.get(MenuItems.DISCONNECT));
        project.add(this.menuItems.get(MenuItems.EXIT));
        
        /*JMenuItem chat = new JMenuItem("Chat");
        chat.addActionListener(new ActionListener() {
        	@Override
        	public void actionPerformed(ActionEvent e) {
        		new __HORRIBLE_CHAT_SAY(controller);
        	}
        });
        
        project.add(chat);*/
        
        // Add the project menu to the menu bar.
        this.add(project);
        
        // Add a place holder settings menu.
        JMenu settings = new JMenu("Settings");
        settings.setMnemonic(KeyEvent.VK_S);
        JMenuItem changeInput = new JMenuItem("Change Input");
        changeInput.setMnemonic(KeyEvent.VK_C);
        changeInput.addActionListener(
                new ActionListener() {
                	@Override
                    public void actionPerformed(ActionEvent e) {
                        controller.switchInput();
                    }
                });
        settings.add(changeInput);
        this.add(settings);
        
        // Create a help menu and add place holder items and separators to it.
        JMenu help = new JMenu("Help");
        help.setMnemonic(KeyEvent.VK_H);
        JMenuItem userGuide = new JMenuItem("User Guide");
        userGuide.setMnemonic(KeyEvent.VK_U);
        userGuide.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
        userGuide.addActionListener(
                new ActionListener() {
                	@Override
                    public void actionPerformed(ActionEvent e) {
                        controller.userGuide();
                    }
                });
        help.add(userGuide);
        JMenuItem contactUs = new JMenuItem("Contact Us");
        contactUs.setMnemonic(KeyEvent.VK_C);
        contactUs.addActionListener(
                new ActionListener() {
                	@Override
                    public void actionPerformed(ActionEvent e) {
                        controller.contactUs();
                    }
                });
        help.add(contactUs);
        help.add(new JSeparator());
        JMenuItem about = new JMenuItem("About");
        about.setMnemonic(KeyEvent.VK_A);
        about.addActionListener(
                new ActionListener() {
                	@Override
                    public void actionPerformed(ActionEvent e) {
                        controller.about();
                    }
                });
        help.add(about);
        
        // Add the help menu to the menu bar.
        this.add(help);
    }

    // Stop save/export being grayed out (when a project is loaded)
    public void enableSaving() {
        this.menuItems.get(MenuItems.SAVE).setEnabled(true);
        this.menuItems.get(MenuItems.HOST).setEnabled(true);
        //this.menuItems.get(MenuItems.EXPORT).setEnabled(true);
    }
    
    public void setConnected() {
    	this.menuItems.get(MenuItems.CONNECT).setEnabled(false);
    	this.menuItems.get(MenuItems.HOST).setEnabled(false);
    	this.menuItems.get(MenuItems.DISCONNECT).setEnabled(true);
    }
    
    public void setDisconnected() {
    	this.menuItems.get(MenuItems.CONNECT).setEnabled(true);
    	this.menuItems.get(MenuItems.HOST).setEnabled(true);
    	this.menuItems.get(MenuItems.DISCONNECT).setEnabled(false);
    }
}
